﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace net.entity.framework.common {
    public static class Utils {
        public static int ComputeHashCode(object one, params object[] many) {
            var hash = ReferenceEquals(one, null) ? 0 : one.GetHashCode();

            foreach (var obj in many) {
                var hash2 = ReferenceEquals(obj, null) ? 0 : obj.GetHashCode();
                hash ^= hash2;
            }
            return hash;
        }

        public static MemberInfo GetMemberInfoByExample<T>(Expression<Func<T>> func) {
            return ((MemberExpression) func.Body).Member;
        }

        public static MethodInfo GetMethodInfoFromExample<T>(Expression<Func<T>> func) {
            return (MethodInfo) GetMemberInfoByExample(func);
        }

        public static FieldInfo GetFieldInfoFromExample<T>(Expression<Func<T>> func) {
            return (FieldInfo) GetMemberInfoByExample(func);
        }

        public static PropertyInfo GetPropertyInfoFromExample<T>(Expression<Func<T>> func) {
            return (PropertyInfo) GetMemberInfoByExample(func);
        }

        internal static void WriteToLog(string text) {
            if (EntityGlobalSettings.Logger != null) {
                EntityGlobalSettings.Logger.Write(text);
            }
        }
    }
}